<cfsetting enablecfoutputonly="true">
<cfswitch expression="#thisTag.executionMode#">
	<cfcase value="start">
		<cfparam name="attributes.id" default="">
		<cfparam name="attributes.name" default="">
		<cfparam name="attributes.data" default="">
		<cfparam name="attributes.valueField" default="value">
		<cfparam name="attributes.displayField" default="display">
		<cfparam name="attributes.label" default="">
		<cfparam name="attributes.labelClass" default="coopFieldLabel">
		<cfparam name="attributes.selected" default="">
		<cfparam name="attributes.innerAttributes" default="#structNew()#">
		<cfparam name="attributes.delimiters" default=",">
		<cfparam name="attributes.fieldParam" default="true">
		<cfparam name="attributes.formatter" type="string" default="">
		<cfparam name="attributes.formatterMask" type="string" default="">
		<cfparam name="attributes.formatterType" type="string" default="">
		<cfparam name="attributes.formatterVersion" type="string" default="">
		<cfif StructKeyExists(caller,'root')>
			<cfset root = caller.root>
		<cfelse>
			<cfset root = caller>
		</cfif>
		<cfif NOT thisTag.hasEndTag>
			<cfthrow detail="End tag required" message="All coop tags require an end tag." errorcode="sos.tag.attributes">
		</cfif>
		<cfif NOT structKeyExists(root,"coop")>
			<cfthrow detail="Missing required COOP object on calling page.">
		</cfif>
		<cfif attributes.id EQ ''>
			<cfthrow detail="Missing required attribute: 'id'" message="'id' attribute is required">
		</cfif>
		<cfscript>
		standardattributes = 'id,formatter,formatterType,formatterVersion,formatterMask,name,data,innerAttributes,displayField,valueField,label,labelClass,delimiters,selected,fieldParam';
		ignoredInnerAttributes = 'id';
		attributes = root.coop.mergeAttributes(attributes,root);
		attributeList = root.coop.createAttributeList(attributes,standardAttributes,root);
		if (attributes.name EQ '') {
			attributes.name = attributes.id;
		}
		if (attributes.fieldParam) {
			attributes.selected = root.coop.fieldParam(root._init.attributes,attributes.name,attributes.selected);
		}
		checkedList = attributes.selected;
		
		thisTag.innerAttributes = arrayNew(1);
		thisTag.innerDataAttributes = structNew();
		innerDataAttributeList = '';
		innerAttributeList = '';
		</cfscript>
	</cfcase>
	<cfcase value="end">
		<cfsavecontent variable="formElement">
			<cfscript>
			if (listLen(structKeyList(attributes.innerAttributes))) {
				if (NOT arrayLen(thisTag.innerAttributes)) {
					thisTag.innerAttributes[1] = structNew();
				}
				structAppend(thisTag.innerAttributes[1],attributes.innerAttributes);
			}
			if (arrayLen(thisTag.innerAttributes)) {
				for (iAttrib in thisTag.innerAttributes[1]) {
					if (left(iAttrib,5) EQ 'data_') {
						thisTag.innerDataAttributes[iAttrib] = thisTag.innerAttributes[1][iAttrib];
						structDelete(thisTag.innerAttributes[1],iAttrib);
					}
				}
			}
			if (arrayLen(thisTag.innerAttributes)) {
				innerAttributeList = root.coop.createAttributeList(thisTag.innerAttributes[1],ignoredInnerAttributes,root);
			}
			if (isSimpleValue(attributes.data) AND attributes.data EQ ''){
				attributes.data = thisTag.generatedContent;
				thisTag.generatedContent = '';
			}
			
			returnData = createObject("component","#root.coop.get_shareClassPath()#objects.coop.returnData").init(appData:attributes.data,delimiters:attributes.delimiters,classPath:#root.coop.get_shareClassPath()#);
			recordCount = returnData.recordCount();
			writeOutput('<select name="#attributes.name#" id="#attributes.id#" multiple="true"#attributeList#>');
			for(iRow=1; iRow LTE recordCount; iRow = iRow + 1) {
				data = returnData.returnDataRowStruct(iRow);
				if (attributes.valueField EQ '')  {
					value = data[1];
				} else {
					if (reFindNoCase("##*##",attributes.valueField)) {
						value = evaluate(attributes.valueField);
					} else {
						value = data[attributes.valueField];
					}
				}
				if (attributes.displayField EQ '')  {
					display = data[1];
				} else {
					if (reFindNoCase("##*##",attributes.displayField)) {
						display = evaluate(attributes.displayField);
					} else {
						display = data[attributes.displayField];
					}
				}
				if (len(attributes.formatter)) {
					display = root.coop._formatter(attributes.formatter,display,attributes);
				}
				innerDataAttributeList = '';
				for (iAttrib in thisTag.innerDataAttributes) {
					if (reFindNoCase("##*##",thisTag.innerDataAttributes[iAttrib])) {
						innerDataAttributeList = listAppend(innerDataAttributeList,'#right(iAttrib,len(iAttrib)-5)#="#evaluate(thisTag.innerDataAttributes[iAttrib])#"'," ");
					} else {
						innerDataAttributeList = listAppend(innerDataAttributeList,'#right(iAttrib,len(iAttrib)-5)#="#data[thisTag.innerDataAttributes[iAttrib]]#"'," ");
					}
				}
				if (innerDataAttributeList NEQ ''){
					innerDataAttributeList = ' ' & innerDataAttributeList;
				}
				if (listFind(checkedList,value)) {
					ischecked = ' selected="selected" ';
				} else {
					ischecked = '';
				}
				writeOutput('<option value="#value#"#isChecked##innerAttributeList##innerDataAttributeList#>#display#</option>');
			}
			writeOutput('</select>');
			</cfscript></cfsavecontent>
			<cfif len(ATTRIBUTES.label)>
				<cfset formElement = '<label for="#attributes.id#"><div class="#attributes.labelClass#">#attributes.label#</div> #formElement#</label>'>
			}>
			</cfif>
		<cfoutput>#formElement#</cfoutput>
	</cfcase>
</cfswitch>
<cfsetting enablecfoutputonly="false">